<?php
/**
* Api_View_Helper_UtilHelper class for Boomerang Referral Network
*
* Api_View_Helper_UtilHelper is the view helper class which is used to provide
* multiple common functionalities for all modules at view pages
* 
* @package      Api_View_Helper
* @copyright    Copyright (c) 2011-2012 Boomerang Referral Network
* @version      $Id: UtilHelper.php v 1.0.0$
* @since        Class available since Release 1.0.0
* @author       Anindya Roy Chodhury <anindya23@gmail.com>
*/
class Api_View_Helper_UtilHelper extends Zend_View_Helper_Abstract
{
    /**
     * Api_View_Helper_UtilHelper instance for Singleton
     * 
     * @static
     * @var Api_View_Helper_UtilHelper 
     */
    private static $_instance = null;

    /**
     * Get Doctrine Entity Manager from Zend_Registry
     * 
     * @return Doctrine\ORM\EntityManager 
     */
    public function getEntityManager()
    {
        return Zend_Registry::get('doctrine')->getEntityManager();
    }

    /**
     * Get Api_View_Helper_UtilHelper instance using Singleton
     * 
     * @static
     * @return Api_View_Helper_UtilHelper 
     */
    public static function utilHelper()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Get module name at view pages
     * 
     * @return string
     */
    public function getModuleName()
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        return ucfirst($request->getModuleName());
    }

    /**
     * Get current invites of the user to show at right panel.
     * 
     * @return Api\Core\Entity\MobMember 
     */
    public function getCurrentInvites()
    {
        $userBll = new Api_Core_Bll_User_User();
        $user = $userBll->getLoggedInUser();
        
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->add('select', 'mb')
                ->add('from', 'Api\Core\Entity\MobMember mb')
                ->where('mb.invitedTo = :user')
                ->andWhere('mb.status = :status')
                ->add('orderBy', 'mb.dateInvited DESC')
                ->setParameter('user', $user->id)
                ->setParameter('status', '0')
                ->setMaxResults(3);
        $result = $qb->getQuery()->getResult();
        return $result;
    }

    /**
     * Get current referral requests of the user to show at right panel.
     * 
     * @return Api\Core\Entity\MobMember 
     */
    public function getCurrentReferralRequests()
    {
        $userBll = new Api_Core_Bll_User_User();
        $user = $userBll->getLoggedInUser();
        
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->add('select', 're')
                ->add('from', 'Api\Core\Entity\ReferralEvent re')
                ->where('re.trustedProvider = :user')
                ->andWhere('re.status = :status')
                ->add('orderBy', 're.dateCreated DESC')
                ->setParameter('user', $user->id)
                ->setParameter('status', 'r')
                ->setMaxResults(3);
        $result = $qb->getQuery()->getResult();
        return $result;
    }

}